package com.tybbank.Activity;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.tybbank.Base.BaseActivity;
import com.tybbank.Bean.TaoCanXQBean;
import com.tybbank.Login.LoginActivity;
import com.tybbank.MyApplication;
import com.tybbank.R;
import com.tybbank.Utils.Constant;
import com.tybbank.Utils.MD5Utils;
import com.tybbank.Utils.MyDialogUtils;
import com.yuyh.library.imgsel.utils.LogUtils;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.SortedMap;
import java.util.TreeMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by Administrator on 2019/3/13.
 */

public class TaoCanDDXQActivity extends BaseActivity {
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_tc_name)
    TextView tvTcName;
    @BindView(R.id.tv_tc_money)
    TextView tvTcMoney;
    @BindView(R.id.tv_tc_time)
    TextView tvTcTime;
    @BindView(R.id.tv_tc_huoqi_time)
    TextView tvTcHuoqiTime;
    @BindView(R.id.tv_top)
    TextView tvTop;
    @BindView(R.id.tv_end)
    TextView tvEnd;
    @BindView(R.id.rl_recycleview)
    RecyclerView rv_recyclerView;
    @BindView(R.id.tv_type_huo)
    TextView tvTypeHuo;
    private String id;
    private List<TaoCanXQBean.OrderListBean> order_list = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_xq);
        ButterKnife.bind(this);
        id = getIntent().getStringExtra("id");
        tvTcName.setText(getIntent().getStringExtra("title"));
        toolbar(toolbar, getString(R.string.chanpin), R.mipmap.left);
        LinearLayoutManager manager = new LinearLayoutManager(this);
        rv_recyclerView.setLayoutManager(manager);
        rv_recyclerView.setHasFixedSize(true);//每个 高度一样
        //创建并设置adapter
        rv_recyclerView.setNestedScrollingEnabled(false);
        init();
    }

    private void init() {
        String time = MD5Utils.getTime();
        SortedMap<String, String> parameters = new TreeMap<String, String>(
                new Comparator<String>() {
                    public int compare(String obj1, String obj2) {
                        // 降序排序
                        return obj1.compareTo(obj2);
                    }
                });
        parameters.put("uuid", MyApplication.uuid);
        parameters.put("token", MyApplication.token);
        parameters.put("id", id);
        parameters.put("TimeStamp", time);
        String sign = MD5Utils.createSign(parameters);
        final Dialog dialog = MyDialogUtils.createLoadingDialog(this, getString(R.string.loading));
        dialog.show();
        OkHttpUtils
                .post()
                .url(Constant.Current_current_order_info)
                .addParams("uuid", MyApplication.uuid)
                .addParams("token", MyApplication.token)
                .addParams("id", id)
                .addParams("TimeStamp", time)
                .addParams("Sign", sign)
                .build()
                .execute(new StringCallback() {
                    @Override
                    public void onError(Call call, Exception e, int id) {
                        e.printStackTrace();
                        dialog.dismiss();
                        Toast.makeText(getBaseContext(), getString(R.string.error), Toast.LENGTH_SHORT).show();
                    }

                    @Override
                    public void onResponse(String response, int id) {
                        LogUtils.e("TAG套餐详情" + response.toString());
                        dialog.dismiss();
                        postReponse(response);
                    }

                    public void postReponse(String json) {
                        try {
                            TaoCanXQBean enterBean = new Gson().fromJson(json, new TypeToken<TaoCanXQBean>() {
                            }.getType());
                            int a = enterBean.getCode();
                            if (a == 1011) {
                                tvTypeHuo.setText(enterBean.getTitle());
                                tvEnd.setText(enterBean.getData().getEnd_time());
                                tvTop.setText(enterBean.getData().getAdd_time());
                                tvTcHuoqiTime.setText(enterBean.getData().getDays());
                                tvTcTime.setText(enterBean.getData().getTerm());
                                tvTcMoney.setText(Html.fromHtml("<big>" + enterBean.getData().getMoney() + "</big>" + " <small>赠送" + enterBean.getData().getMoney_reward() + "</small>"));
                                order_list = enterBean.getOrder_list();

                                //商品
                                buylistdapter adapte = new buylistdapter();
                                rv_recyclerView.setAdapter(adapte);
                                adapte.notifyDataSetChanged();
                            } else if (a == 1004) {
                                Intent intent = new Intent(getBaseContext(), LoginActivity.class);
                                startActivity(intent);
                                return;
                            }
                        } catch (Exception e) {

                        }

                    }
                });

    }


    public class buylistdapter extends RecyclerView.Adapter {
        @Override
        public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(getBaseContext()).inflate(R.layout.taocan_xx_adapter_item, parent, false);
            return new MYholder(view);
        }

        @Override
        public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
            MYholder holder1 = (MYholder) holder;
            holder1.setdata(position);
        }

        @Override
        public int getItemCount() {
            return order_list.size();
        }

        private class MYholder extends RecyclerView.ViewHolder {

            private final TextView tv_num, tv_end1, tv_end;
            private final ProgressBar progressBar;

            public MYholder(View view) {
                super(view);
                tv_num = view.findViewById(R.id.tv_num);
                progressBar = view.findViewById(R.id.progressBar);
                tv_end1 = view.findViewById(R.id.tv_end1);
                tv_end = view.findViewById(R.id.tv_end);
            }

            public void setdata(final int position) {
                tv_num.setText(order_list.get(position).getRatio());
                tv_end1.setText(order_list.get(position).getEnd_time());
                tv_end.setText(order_list.get(position).getMoney());
                progressBar.setProgress(Integer.parseInt(order_list.get(position).getTime_scale()));
            }
        }
    }
}
